<template>
  <el-form :model="detailForm" ref="detailForm" :rules="rules" :inline="false"
           :class="isEdit?'':'formItem'" :label-position="labelPosition">

    <!-- 收取加速折旧费 -->
    <el-card class="box-card" shadow="never">

      <div slot="header" class="header-card">
        <span class="header-card-title">收取加速折旧费</span>
      </div>

      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="加速折旧费方案:" prop="depreciationPlanId">
            <el-select style="width: 100%" v-if="isEdit" v-model="detailForm.depreciationPlanId"
                       :disabled="!detailForm.vehicleAssessmentPrice"
                       @change="handleChangeDepreciation"
                       placeholder="请选择加速折旧费方案">
              <el-option v-for="item in depreciationList" :key="item.id" :label="item.planName" :value="item.id"/>
            </el-select>
            <span v-else>{{ detailForm.depreciationPlan }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="加速折旧费金额:" prop="depreciationPrice">
            <el-input v-if="isEdit" v-model="detailForm.depreciationPrice" disabled
                      @change="changeAmount('depreciationPrice')" v-thousand
                      placeholder="加速折旧费金额"/>
            <span v-else>{{ format_thousand(detailForm.depreciationPrice) }}</span>
          </el-form-item>
        </el-col>

      </el-row>
    </el-card>

  </el-form>
</template>

<script>
import {getList} from "@/api/cwgd/insuranceSelect";

export default {
  name: "accelerated", //收取加速折旧费
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: "",
    },
  },

  computed: {
    detailForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.isEdit) {
        return "top";
      } else {
        return "left";
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    },
  },
  watch: {},
  data() {
    return {
      rules: {
        depreciationPlanId: [{required: this.state === 'write', message: '请选择加速折旧费方案', trigger: 'change'}],
        depreciationPrice: [{required: this.state === 'write', message: '请输入加速折旧费金额', trigger: 'blur'}],
      },
      depreciationList: [],
      depreciation: {}
    };
  },
  created() {
  },
  mounted() {
    this.handleDepreciationPlan()
  },
  methods: {

    handleDepreciationPlan() {
      getList().then(res => {
        if (res.code === 200) {
          this.depreciationList = res.data || []
        }
      })
    },

    handleChangeDepreciation(val) {
      if (val) {
        this.depreciation = this.depreciationList.filter(i => i.id === val)[0]
        this.value.depreciationPlanId = this.detailForm.depreciationPlanId = val
        this.value.depreciationPlan = this.detailForm.depreciationPlan = this.depreciation.planName
        let num = (this.value.vehicleAssessmentPrice - Number(this.depreciation.deductionAmount)) * Number(this.depreciation.scale) / 100
        console.log(num,this.value.vehicleAssessmentPrice , '’-=-=-=-=-=-=')
        this.value.depreciationPrice = this.detailForm.depreciationPrice = num > 0 ? num : 0
      }
    },

    changeAmount(str) {
      let msg = /^(?:[1-9]\d*|0)(?:\.\d+)?$/;
      if (!msg.test(this.detailForm[str])) {
        this.detailForm[str] = "";
      } else {
        this.detailForm[str] = this.detailForm[str]
            .toString()
            .match(/^\d+(?:\.\d{0,2})?/)[0];
      }
    },
    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.detailForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            this.locateToErr()
            reject();
          }
        });
      })
    },

    locateToErr() {

      this.$nextTick(() => {
        let isError = document.getElementsByClassName('is-error')
        isError[0].scrollIntoView({
          // 滚动到指定节点
          // 值有start,center,end，nearest，当前显示在视图区域中间
          block: 'center',
          // 值有auto、instant,smooth，缓动动画（当前是慢速的）
          behavior: 'smooth',
        })
      })
    },

    valid() {
      return new Promise((resolve) => {
        this.$refs.detailForm.validate((valid) => {
          resolve(valid);
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";
</style>



